<?php
/**
 * Created by PhpStorm.
 * User: luckystar_D
 * Date: 2017/1/21
 * Time: 上午11:44
 */

namespace api\modules\v1\models;
use Yii;
use linslin\yii2\curl\Curl;
/*
 * 上海常悦流量充值接口
 *
 * 订单状态 $status  0:待充值;
 *                  1:已充值;
 *                  2:充值成功;
 *                  3:充值失败;
 *                  4:未知;
 * 接口状态 $code     0:操作成功;
 *                  1:系统升级中;
 *                  -1:参数错误;
 *                  -2:访问超速;
 *                  -3:鉴权失败(校验码错误、ip白名单、ip黑名单);
 *                  -4:账号异常（账号禁用）;
 *                  -5:异常订购（号码与产品不匹配、无效的产品代码）;
 *                  -6:余额不足;
 *                  -7:系统异常;
 * 接口类型:POST;
 * 接口参数:@$param = array(
 *              'apikey' => 'qlkhqpttflvas7psj75abbwkwrhgnjg8', //平台用户唯一标示
 *              'sign' => 'abaa4537af7b23e80236ea4bc0e5ed16',//鉴权验证MD5($param['apikey'] . $param['username']);
 *              'phone_no' => '18646546800',//手机号码
 *              'flow_val' => '100',//充值流量
 *              'out_order_id' => 'SH20170118123456', //外部订单号
 *              'timetamp' => '12312312312', //时间戳
 *              'scope' => '0'//0=>国内/1=>省内
 *          );
 */

class ChangYue
{
    public static $app_id = 'Gl3FPNKc3r';
    public static $app_secret = 'IHOljUKaHopWmqDpHlai';
    public static $submit_url = 'http://api.23456g.com/api/v2/order/submit.json';
    public static $blance_url = 'http://api.23456g.com/api/v1/account/queryblance.json';
    public static $query_url = 'https://api.23456g.com/api/v1/order/query.json';

    public static function submit($post)
    {
        $sign = self::sign();
        $param = array(
             'app_id' => self::$app_id,
             'verify_sign' => $sign,
             'out_order_id' => $post['out_order_id'],
             'flow_val' => $post['flow_val'],
             'phone_no' => $post['phone_no'],
             'timestamp' => date('YmdHmsB', time()),
             'scope' => $post['scope'] ? $post['scope'] : "0",
        );
        $return = self::curl_http(self::$submit_url, $param);
        $return_array = json_decode($return, true);
        $data_array = array(
            'out_order_id' => $post['out_order_id'], 
            'flow_val' => $post['flow_val'], 
            'phone_no' => $post['phone_no'],
            'order_id' => !empty($return_array['order_id']) ? $return_array['order_id'] : '',
        );
        return self::returncode($return_array['result'], $data_array);
    }
    //余额查询接口
    public static function blance()
    {
        $sign = self::sign();
        $param = array(
            'app_id' => self::$app_id,
            'verify_sign' => $sign
        );
        $return = self::curl_http(self::$blance_url, $param);
        $return_array = json_decode($return, true);
        return self::returncode(0,$return_array);
    }
    //订单查询接口
    public static function query($post)
    {
        $sign = self::sign();
        $param = array(
            'app_id' => self::$app_id,
            'verify_sign' => $sign,
            'out_order_id' => $post['out_order_id'],
        );
        $return = self::curl_http(self::$query_url, $param);
        $return_array = json_decode($return, true);
        return self::querycode($return_array['status']);
    }
    //回调地址接口
    public function back()
    {
        /*
         *
         */
    }
    //接口sign验证规则
    public static function sign(){
        return MD5(self::$app_id . self::$app_secret);
    }
    //curl_http
    public static function curl_http($url,$param){
        $curl = new Curl();
        $response = $curl->setOption(
            CURLOPT_POSTFIELDS,
            http_build_query($param)
        )->post($url);
        return $response;
    }
    public static function querycode($code = 0, $data = array())
    {
        switch ($code)
        {
            case 0 :
                $returncode = 1;
                $message = '待充值';
                break;
            case 1 :
                $returncode = 2;
                $message = '已充值';
                break;
            case 2 :
                $returncode = 3;
                $message = '充值成功';
                break;
            case 3 :
                $returncode = 4;
                $message = '充值失败';
                break;
            case 4 :
                $returncode = 5;
                $message = '未知';
                break;
            default :
                $returncode = 1;
                $message = '待充值';
                break;
        }
        return json_encode([
            'code' => $returncode,
            'message' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        //Yii::$app->end();
    }
    public static function returncode($code = 0, $data = array())
    {
        switch ($code)
        {
            case 0 :
                $returncode = 0;
                $message = '处理成功';
                break;
            case 1 :
                $returncode = 1;
                $message = '系统升级中';
                break;
            case -1 :
                $returncode = -1;
                $message = '参数错误';
                break;
            case -2 :
                $returncode = -5;
                $message = '网络异常';
                break;
            case -3 :
                $returncode = -3;
                $message = '鉴权错误';
                break;
            case -4 :
                $returncode = -7;
                $message = '系统异常';
                break;
            case -5 :
                $returncode = -8;
                $message = '通道异常';
                break;
            case -6 :
                $returncode = -6;
                $message = '接口账户余额不足';
                break;
            case -7 :
                $returncode = -7;
                $message = '系统异常';
                break;
            default :
                $returncode = 0;
                $message = '处理成功';
                break;
        }
        return json_encode([
            'code' => $returncode,
            'message' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        //Yii::$app->end();
    }


}